import gdata.spreadsheet.service
import sys
from time import time

def benchmark(message, f, *E, **F):
  print "%s" % message.ljust(40),
  s = time()
  r = f(*E, **F)
  print "-- %.2f seconds" % (time()-s)
  return r

def make_client(user, passwd):
  client = gdata.spreadsheet.service.SpreadsheetsService()
  client.source = "UOIT Pages"
  client.email = user
  client.password = passwd
  client.ProgrammaticLogin()
  return client

def get_spreadsheet_key():
  return '0AvCncvgg6JftdE85RHFxeGE3OVo1LUhNTmE4d0J3UXc'

def make_worksheet_feed(client):
  return client.GetWorksheetsFeed(get_spreadsheet_key())

def get_worksheet_keys(feed):
  return [x.id.text.split("/")[-1] for x in feed.entry]

def make_list_feed(client, spreadsheet_key, worksheet_key):
  listfeed = client.GetListFeed(spreadsheet_key, worksheet_key)
  return listfeed

if __name__ == '__main__':
  (user, pw) = sys.argv[1:]
  client = benchmark("make_client", make_client, user, pw)
  sheet = benchmark("make_worksheet_feed", make_worksheet_feed, client)
  print "\tLast update is %s" % sheet.updated.text
  for worksheet_key in get_worksheet_keys(sheet):
    benchmark('make_list_feed', make_list_feed,
        client, get_spreadsheet_key(), worksheet_key)
